package com.edu.zut.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * @Author 许文科
 * @Date 2023/1/12 15:13
 * @Description paperdetect-bcak
 * <p>
 * 代码生成器
 */
public class CodeGenerator {

    //配置数据源
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG =
            new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/paperdetect?serverTimezone=GMT%2B8", "root", "xuwenke")
                    .typeConvert(new MySqlTypeConvert());

    public static void main(String[] args) {

        String projectPath = System.getProperty("user.dir");

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(builder -> {
                    builder.author("许文科")
                            //.enableSwagger()
                            .disableOpenDir()//执行完毕不打开文件夹
                            .fileOverride()//覆盖已生成的文件
                            .outputDir(projectPath + "/src/main/java");//指定输出目录
                })
                //包配置
                .packageConfig(builder -> {
                    builder.parent("com.edu.zut")//设置父包名
                            .controller("controller")//生成controller层
                            .entity("entity")//实体层
                            .service("service")//服务层
                            .serviceImpl("service.Impl")
                            .mapper("mapper");
                })
                //策略配置
                .strategyConfig(builder -> {
                    builder.addInclude("sys_result")//设置生成的表
                            .addTablePrefix("sys_")//过滤前缀
                            .serviceBuilder()//开启service策略配置
                            .formatServiceFileName("%sService")//取消Service前的I
                            .controllerBuilder()//开启controller策略配置
                            .enableRestStyle()//restful风格api
                            .enableHyphenStyle()//url中驼峰转连字符
                            .entityBuilder()//开启实体类配置
                            .enableLombok()//开启lombok
                            .enableChainModel();//开启lombok链式操作
                })
                //模板配置
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
